<template>
	<view>
		<!-- 已登录 -->
		<navigator v-if="$store.state.userInfo" :url="'/pages/user-space/user-space?user_id='+$store.state.userInfo.id">
			<view class="flex align-center p-2" hover-class="bg-light">
				<image 
				:src="$store.state.userInfo.avatar" 
				style="width: 100rpx;height: 100rpx;" 
				class="rounded-circle" mode="aspectFill"></image>
				<view class="flex flex-1 px-2 flex-column">
					<text class="font-lg font-weight-bold text-dark">{{$store.state.userInfo.username}}</text>
					<text class="font text-muted">总帖子{{posts.posts_total}} 今日发帖{{posts.today}}</text>
				</view>
				<text class="iconfont icon-jinru"></text>
			</view>
		</navigator>
		<!-- 未登录 -->
		<navigator v-else url="/pages/login/login">
			<view class="flex align-center p-2" hover-class="bg-light">
				<image 
				src="/static/default.jpg" 
				style="width: 100rpx;height: 100rpx;" 
				class="rounded-circle" mode="aspectFill"></image>
				<view class="flex flex-1 align-center">
					<view class="ml-2 font-md">未登录</view>
				</view>
				<text class="iconfont icon-jinru"></text>
			</view>
		</navigator>
		
		<view class="flex align-center px-3 py-2">
			<view 
			class="flex-1 flex flex-column align-center justify-center" 
			v-for="(item,index) in myData" :key="index">
				<text class="font-lg font-weight-bold">{{item.num}}</text>
				<text class="font text-muted">{{item.name}}</text>
			</view>
		</view>
		<view class="px-3 py-2">
			<image src="/static/demo/datapic/20.jpg" 
			mode="aspectFill" 
			style="height: 170rpx;width: 100%;" class="rounded"></image>
		</view>
		<uni-list-item title="浏览历史" showExtraIcon @click="openHistory">
			<text slot="icon" class="iconfont icon-liulan"></text>
		</uni-list-item>
		<uni-list-item title="社区认证" showExtraIcon>
			<text slot="icon" class="iconfont icon-huiyuanvip"></text>
		</uni-list-item>
		<uni-list-item title="审核帖子" showExtraIcon>
			<text slot="icon" class="iconfont icon-keyboard"></text>
		</uni-list-item>
	</view>
</template>

<script>
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue'
	import {getUserNews} from "@/network/login.js";
	export default {
		components:{
			uniListItem
		},
		onLoad() {
			getUserNews().then(attr=>{
				let [err,res] = attr
				if(res.statusCode==403){
					return
				}
				this.myData = res.data.data.total
				this.posts = res.data.data.posts
			})
		},
		onShow() {
			this.getCounts()
		},
		data() {
			return {
				myData:[{
					name: "帖子",
					num: 0
				}, {
					name: "动态",
					num: 0
				}, {
					name: "评论",
					num: 0
				}, {
					name: "粉丝",
					num: 0
				}],
				posts:{
					"today":0,
					"posts_total":0
				}
			}
		},
		onNavigationBarButtonTap() {
			uni.navigateTo({
				url:"/pages/user-set/user-set"
			})
		},
		methods: {
			openHistory(){
				uni.navigateTo({
					url: '/pages/history/history'
				});
			},
			getCounts(){
				getUserNews().then(attr=>{
					let [err,res] = attr
					if(res.statusCode==403){
						this.myData.forEach(item=>{
							item.num = 0
						})
						return
					}
					this.myData = res.data.data.total
					this.posts = res.data.data.posts
				})
			}
		}
	}
</script>

<style>

</style>
